﻿Public Class Incidents
    Public Shared IncidentsDataSet As DataSet = New DataSet
    Dim DatabaseFuncs As New DatabaseFuncs

    'Variabelen om een gebruiker op te halen:
    Public _Id As Integer
    Public ReadOnly Property Id() As Integer
        Get
            Id = _Id
        End Get
    End Property

    Private _ovnummer As String
    Public Property ovnummer() As String
        Get
            ovnummer = _ovnummer
        End Get
        Set(ByVal value As String)
            _ovnummer = value
        End Set
    End Property

    Private _apparaat As String
    Public Property apparaat() As String
        Get
            apparaat = _apparaat
        End Get
        Set(ByVal value As String)
            _apparaat = value
        End Set
    End Property

    Private _categorie As String
    Public Property categorie() As String
        Get
            categorie = _categorie
        End Get
        Set(ByVal value As String)
            _categorie = value
        End Set
    End Property

    Private _beschrijving As String
    Public Property beschrijving() As String
        Get
            beschrijving = _beschrijving
        End Get
        Set(ByVal value As String)
            _beschrijving = value
        End Set
    End Property

    Private _status As String
    Public Property status() As String
        Get
            status = _status
        End Get
        Set(ByVal value As String)
            _status = value
        End Set
    End Property

    Private _oplossing As String
    Public Property oplossing() As String
        Get
            oplossing = _oplossing
        End Get
        Set(ByVal value As String)
            _oplossing = value
        End Set
    End Property

    Private _behandelaar As String
    Public Property behandelaar() As String
        Get
            behandelaar = _behandelaar
        End Get
        Set(ByVal value As String)
            _behandelaar = value
        End Set
    End Property

    Private _eerstebehandelaar As String
    Public Property eerstebehandelaar() As String
        Get
            eerstebehandelaar = _eerstebehandelaar
        End Get
        Set(ByVal value As String)
            _eerstebehandelaar = value
        End Set
    End Property

    Private _prioriteit As String
    Public Property prioriteit() As String
        Get
            prioriteit = _prioriteit
        End Get
        Set(ByVal value As String)
            _prioriteit = value
        End Set
    End Property

    Private _impact As String
    Public Property impact() As String
        Get
            impact = _impact
        End Get
        Set(ByVal value As String)
            _impact = value
        End Set
    End Property

    Private _deadline As String
    Public Property deadline() As String
        Get
            deadline = _deadline
        End Get
        Set(ByVal value As String)
            _deadline = value
        End Set
    End Property

    Private _aangemeld As String 'datum!
    Public Property aangemeld() As String
        Get
            aangemeld = _aangemeld
        End Get
        Set(ByVal value As String)
            _aangemeld = value
        End Set
    End Property

    Private _tijd As String 'aangemeld tijd!
    Public Property tijd() As String
        Get
            tijd = _tijd
        End Get
        Set(ByVal value As String)
            _tijd = value
        End Set
    End Property

    Private _extra As String 'extra info
    Public Property extra() As String
        Get
            extra = _extra
        End Get
        Set(ByVal value As String)
            _extra = value
        End Set
    End Property

    Private _topdesk As String 'topdesk nummer
    Public Property topdesk() As String
        Get
            topdesk = _topdesk
        End Get
        Set(ByVal value As String)
            _topdesk = value
        End Set
    End Property

    Public Function getDate()
        Dim regDate As Date = Date.Now()
        Dim Datum As String = regDate.ToString("dd-MM-yyyy")
        Return Datum

    End Function

    Public Sub LoadFromDB(ByVal incid_id As String)
        Try
            Dim queryresult As String = ""
            queryresult = DatabaseFuncs.query("SELECT * FROM `incidenten` WHERE `id`=" + incid_id, 3)

            If Not queryresult = "True" Then 'De query heeft een fout.
                MsgBox("Laden informatie uit Incidentendatabase is mislukt (fout vanuit query): " + queryresult)
                Exit Sub
            End If

            'If Not IncidentsDataSet.Tables("Table").Rows.Count = 0 Then
            Dim DR As DataRow = IncidentsDataSet.Tables("Table").Rows(0)
            _Id = Convert.ToInt32(DR.Item("id").ToString)
            _ovnummer = DR.Item("ovnummer").ToString
            apparaat = DR.Item("apparaat").ToString
            categorie = DR.Item("categorie").ToString
            beschrijving = DR.Item("beschrijving").ToString
            status = DR.Item("status").ToString
            oplossing = DR.Item("oplossing").ToString
            behandelaar = DR.Item("behandelaar").ToString
            prioriteit = DR.Item("prioriteit").ToString
            impact = DR.Item("impact").ToString
            deadline = DR.Item("deadline").ToString
            eerstebehandelaar = DR.Item("eerstebehandelaar").ToString
            aangemeld = DR.Item("aangemeld").ToString
            tijd = DR.Item("tijd").ToString
            extra = DR.Item("extra").ToString
            topdesk = DR.Item("topdesk").ToString
            'deadline = Convert.ToDateTime(DR.Item("deadline").ToString("0000-00-00")) 'datetime: 0000-00-00
            'aangemeld = Format("yyyy-MM-dd hh:mm:ss", DR.Item("aangemeld").ToString) 'timestamp: 0000-00-00 00:00:00
            'End If

        Catch ex As Exception
            MsgBox("Laden informatie uit Usersdatabase is mislukt (fout bij variabelen): " + ex.Message)
        End Try
    End Sub

    Public Sub clearData()
        _Id = Nothing
        _ovnummer = Nothing
        apparaat = Nothing
        categorie = Nothing
        beschrijving = Nothing
        status = Nothing
        oplossing = Nothing
        behandelaar = Nothing
        prioriteit = Nothing
        impact = Nothing
        deadline = Nothing
        eerstebehandelaar = Nothing
        aangemeld = Nothing
        tijd = Nothing
        extra = Nothing
        topdesk = Nothing
    End Sub

    Public Function LoadAllFromDB(Optional ByVal klant_ovnr As String = Nothing)
        Dim queryresult As String
        If klant_ovnr = Nothing Then
            queryresult = DatabaseFuncs.query("SELECT * FROM `incidenten` ORDER BY `id`", 3)
        Else
            queryresult = DatabaseFuncs.query("SELECT * FROM `incidenten` WHERE `ovnummer` = '" + klant_ovnr.ToString + "' ORDER BY `id`", 3)
        End If

        If Not queryresult = "True" Then
            Return queryresult
        End If
        Return "True"
    End Function

    Public Sub SaveToDB(ByVal id As String, ByVal Nieuwincident As Boolean) 'deadline, aangemeld en tijd zijn weggelaten!!!
        If Nieuwincident = True Then
            DatabaseFuncs.query("INSERT INTO `incidenten` (ovnummer, apparaat, categorie, beschrijving, status, oplossing, behandelaar, prioriteit, impact, deadline, eerstebehandelaar, extra, topdesk) VALUES ('" + ovnummer.ToString + "', " + apparaat.ToString + ", " + categorie.ToString + ", '" + beschrijving.ToString + "', '" + status.ToString + "', '" + oplossing.ToString + "', '" + behandelaar.ToString + "', '" + prioriteit.ToString + "', '" + impact.ToString + "', '" + eerstebehandelaar.ToString + "', '" + extra.ToString + "', '" + topdesk.ToString + "')", 3)
        Else
            DatabaseFuncs.query("UPDATE `incidenten` SET `ovnummer` = '" + ovnummer.ToString + "', " _
            + "`apparaat` = '" + apparaat.ToString + "', `categorie` = '" + categorie.ToString + "', `beschrijving` = '" + beschrijving.ToString + "', `status` = '" + status.ToString + "', `oplossing` = '" + oplossing.ToString + "', `behandelaar` = '" + behandelaar.ToString + "', `prioriteit` = '" + prioriteit.ToString() + "', `impact` = '" + impact.ToString + "', `eerstebehandelaar` = '" + eerstebehandelaar.ToString() + "', `extra` = '" + extra.ToString() + "', `topdesk` = '" + topdesk.ToString() + "' WHERE id=" + id.ToString, 3)
        End If
    End Sub

    Public Function DeleteFromDB(ByVal id As String)
        Dim queryresult As String = DatabaseFuncs.query("DELETE FROM `incidenten` WHERE `id`=" + id, 3)
        If Not queryresult = "True" Then
            Return queryresult
        End If
        clearData()
        Return "True"
    End Function
End Class
